package com.yc.dao;

import com.yc.bean.FoodBean;
import com.yc.bean.FoodDetailBean;
import com.yc.bean.RepliesBean;
import com.yc.bean.ReviewBean;
import com.yc.commons.DBHelper;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

public class FoodDao {
    static DBHelper db=new DBHelper();

    public static List<FoodBean> find() {
        String sql="select * from tb_food where disabled=0";
        try {
            return db.select(FoodBean.class, sql);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public static List<FoodBean> find(int id) {
        String sql="select * from tb_food where disabled=0 and id=?";
        try {
            return db.select(FoodBean.class, sql,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void update(int foodId, int buyNum) {
        //更新菜品的销量
        String sql="UPDATE tb_food\n" +
                "SET totalbuynum = totalbuynum + ?\n" +
                "WHERE id=?;\n";
        db.doUpdate(sql,  buyNum,foodId);

    }

    public static List<FoodDetailBean> finddetail(int foodid) {
            String sql="select * from tb_food_detail where foodid=? and disabled=0";
        try {
            return db.select(FoodDetailBean.class,sql,foodid);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    public static List<ReviewBean> findreview(int foodid) {
        String sql="select * from tb_food_reviews where foodid=?  order by createdate DESC";
        try {
            return db.select(ReviewBean.class,sql,foodid);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    public static Boolean insertReview(int userid,int foodid,String reviewText, String url,int star) {
        String sql="insert into tb_food_reviews (userid,foodid,review_text,review_image_url,createdate,star,disabled)" +
                "values(?,?,?,?,NOW(),?,0)";
        int i = db.doUpdate(sql, userid, foodid, reviewText, url, star);
        if (i>0){
            return true;
        }
        return false;
    }

    public static Boolean insertReplies(int review_id,int userid,String reply_text, String url) {
        String sql="INSERT INTO tb_review_replies (review_id, user_id, reply_text, reply_image_url, createdate,disabled)\n" +
                "VALUES (?, ?, ?, ?, NOW(),0);\n";
        int i = db.doUpdate(sql, review_id,userid,reply_text, url);
        if (i>0){
            return true;
        }
        return false;
    }

    public static List<RepliesBean> findreplies() {
        String sql="select * from tb_review_replies order by createdate DESC ";
        try {
            return db.select(RepliesBean.class, sql);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
